#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/11/5 16:31
# @Author  : 王凯
# @File    : pmi_clean.py
# @Project : scrapy_spider
import datetime

from apps.data_stats.data_stats.clean import BaseClean


class PmiClean(BaseClean):

    def save_data(self, datas, table="net_manufacturing_pmi"):
        batch_size = 1000
        for i in range(0, len(datas), batch_size):
            self.to_db.add_batch_smart(table, list(datas[i:i + batch_size]), update_columns=list(datas[0].keys()))

    def deal_df_one(self, df_one, name_mapping):
        item = df_one[["tag_name", "num"]].set_index("tag_name").T.rename(columns=name_mapping).to_dict("index")['num']
        item.update({"time": df_one['date_string'].tolist()[0]})
        return item

    def run_manufacturing(self):
        df = self.get_data_from_db(cate_1="采购经理指数", cate_2="制造业采购经理指数", condition=" and area = '全国' ")
        df = df[df['date_string'].str.startswith(tuple(str(i) for i in range(2005, datetime.datetime.now().year + 1)))]
        name_mapping = {
            "主要原材料购进价格指数": "main_raw_material_purchase_price_index",
            "产成品库存指数": "finished_product_inventory_index",
            "从业人员指数": "employee_index",
            "供应商配送时间指数": "supplier_delivery_time_index",
            "出厂价格指数": "producer_price_index",
            "制造业采购经理指数": "manufacturing_pmi",
            "原材料库存指数": "raw_material_inventory_index",
            "在手订单指数": "on_hand_order_index",
            "新出口订单指数": "new_export_order_index",
            "新订单指数": "new_order_index",
            "生产指数": "production_index",
            "生产经营活动预期指数": "produce_operate_activity_expect_index",
            "进口指数": "import_index",
            "采购量指数": "purchasing_volume_index",
        }
        res = df.groupby("date_string").apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "net_manufacturing_pmi")

    def run_non_manufacturing(self):
        df = self.get_data_from_db(cate_1="采购经理指数", cate_2="非制造业采购经理指数", condition=" and area = '全国' ")
        df = df[df['date_string'].str.startswith(tuple(str(i) for i in range(2007, datetime.datetime.now().year + 1)))]
        name_mapping = {
            "非制造业商务活动指数": "non_manufacturing_business_activity_index",
            "新订单指数": "new_order_index",
            "新出口订单指数": "new_export_order_index",
            "在手订单指数": "on_hand_order_index",
            "存货指数": "inventory_index",
            "投入品价格指数": "input_price_index",
            "销售价格指数": "sales_price_index",
            "从业人员指数": "employee_index",
            "供应商配送时间指数": "supplier_delivery_time_index",
            "业务活动预期指数": "business_activity_expectation_index",
            "建筑业商务活动指数": "construction_industry_business_activity_index",
            "服务业商务活动指数": "service_industry_business_activity_index",
        }
        res = df.groupby("date_string").apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "net_non_manufacturing_pmi")

    def run_comprehensive(self):
        df = self.get_data_from_db(cate_1="采购经理指数", cate_2="综合PMI产出指数", condition=" and area = '全国' ")
        df = df[df['date_string'].str.startswith(tuple(str(i) for i in range(2017, datetime.datetime.now().year + 1)))]
        name_mapping = {
            "综合PMI产出指数": "comprehensive_pmi_output_index",
        }
        res = df.groupby("date_string").apply(lambda df_one: self.deal_df_one(df_one, name_mapping)).tolist()
        self.save_data(res, "net_comprehensive_pmi_output_index")

    def run(self):
        self.run_manufacturing()
        self.run_non_manufacturing()
        self.run_comprehensive()


if __name__ == "__main__":
    PmiClean().run()
